package com.iflytek.util;

import org.springframework.stereotype.Component;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.stream.Stream;

import static com.iflytek.config.StartupRunner.log;

@Component
public class GetFileName {

    public static String getSingleFileName(String directoryPath) {
        try (Stream<Path> files = Files.list(Paths.get(directoryPath))) {
            Optional<Path> file = files.filter(Files::isRegularFile).findFirst();
            return file.map(path -> path.getFileName().toString()).orElse(null);
        } catch (Exception e) {
            log.error("读取目录 {} 失败", directoryPath, e);
            return null;
        }
    }
}
